home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1695 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: news.mindlink.net!news
  2. From: genew@mindlink.bc.ca (Gene Wirchenko)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer-to-Double as Function Arg
  5. Date: Tue, 16 Jan 1996 10:49:24 GMT
  6. Organization: MIND LINK! - British Columbia, Canada
  7. Message-ID: <4dfvu4$q18@fountain.mindlink.net>
  8. References: <4dfccl$j5h@colossus.holonet.net>
  9. NNTP-Posting-Host: line023.nwm.mindlink.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. mitch@news.mdli.com (Mitch Miller) wrote:
  13.  
  14. >I'm having a problem with a function that takes a
  15. >couple of pointer-to-double arguments, initializes then
  16. >and assigns values to them in an array style:
  17.  
  18. >Function is something like:
  19.  
  20. >    int GetValue( long iDim, double ** Values1, double **
  21. Above line changed from "*" to "**" twice
  22. >      Values2 )
  23. >    {
  24. >        int iter;
  25. >        *Values1 = (double *) malloc( iDim * sizeof( double ));
  26. Dereference Values1
  27. >        if (Values1 == NULL )
  28. >            ....
  29. >        *Values2 = (double *) malloc( iDim * sizeof( double ));
  30. Dereference Values2
  31. >        if (Values2 == NULL )
  32. >            ...
  33.  
  34. >        for (iter=1; iter<iDim; iter++)
  35. >        {
  36. >            *Values1[iter] = ...;
  37. Dereference Values1
  38. >            *Values2[iter] = ...;
  39. Dereference Values2
  40. >        }
  41.  
  42. >        return 1;
  43. >    }
  44.  
  45. >Call from main:
  46.  
  47. >    double ** Ptr1;
  48. Double indirection
  49. >    double ** Ptr2;
  50. Double indirection
  51. >    if (!GetValue( iSize, Ptr1, Ptr2 ) )....
  52.  
  53.  
  54. >When I look at the values of Values1 and Values2 in GetValue, they have
  55. >the correct values.
  56.  
  57.      Yes, call by value allows the LOCAL parameter value to be
  58. changed...
  59.  
  60. >However, back in main, they have NULL values.
  61.  
  62.      ...but does not change the actual parameter.  It might not be a
  63. NULL value, could be anything.
  64.  
  65. >If I make Ptr1 and Ptr2 global variables so that they are not included
  66. >in the function calls, the code works fine.
  67.  
  68.      Yes, then you aren't using calling, so call by value doesn't get
  69. you.
  70.  
  71. >I've checked the FAQs, but couldn't find anything quite like this.
  72.  
  73.      It's probably in there somewhere.
  74.  
  75. >Thanks in advance... 
  76.  
  77.      In C, parameters are passed by value.  In order to modify a var
  78. in a function and have it stick, the var must be global or if a parm,
  79. it must be a pointer to the var.
  80.      So for a pointer to double to be changeable, call with..."All
  81. together, class!"... a pointer to pointer to double.
  82.  
  83. Sincerely,
  84.  
  85. Gene Wirchenko
  86.  
  87. C Pronunciation Guide:
  88.      y=x++;     "wye equals ex plus plus semicolon"
  89.      x=x++;     "ex equals ex doublecross semicolon"
  90.  
  91.